package mock.bo;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import mock.dao.ATMDAO;
import mock.dao.ATMDAOImp;
import mock.dao.ConfigDAO;
import mock.dao.ConfigDAOImp;
import mock.dao.CustomerDAO;
import mock.dao.CustomerDAOImp;
import mock.dao.LogTransferDAO;
import mock.dao.LogTransferDAOImp;
import mock.dao.LogTypeDAO;
import mock.dao.LogTypeDAOImp;
import mock.dto.ATMDTO;
import mock.dto.ConfigDTO;
import mock.dto.CustomerDTO;
import mock.dto.LogTransferDTO;
import mock.dto.LogViewDTO;
import mock.utility.Utility;

public class ViewHistoryImp implements ViewHistory{
	
	private Utility util = new Utility();
	private ConfigDAO configDAO = new ConfigDAOImp();
	private LogTransferDAO logTransferDAO = new LogTransferDAOImp();
	private LogTypeDAO logTypeDAO = new LogTypeDAOImp();
	private CustomerDAO customerDAO = new CustomerDAOImp();
	private ATMDAO atmDAO = new ATMDAOImp();
	
	/**
	 * tinh toan thoi gian bat dau lay du lieu
	 */
	@Override
	public Date calculateStartDate(String subDate) {
		// TODO Auto-generated method stub
		//chuyen ngay hien tai ve string
		String curDate = util.getCurrentDate().toString();
		String[] currentDate = curDate.split("-");
		//tach thanh tung thanh phan co ban
		int year = Integer.parseInt(currentDate[0]);
		int month = Integer.parseInt(currentDate[1]);
		int day = Integer.parseInt(currentDate[2]);
		//phan tich thanh phan input
		char number = subDate.charAt(0);
		char type = subDate.charAt(1);
		switch(type){
		case 'w':
			//tinh so ngay
			int numw = 7 * (Integer.parseInt(String.valueOf(number)));
			//tinh toan
			day = day - numw;
			if(day <= 0){
				month = month - 1;
				if(month == 0 || month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){
					day = day + 31;
				}else if(month == 2){
					day = day + 28;
				}else{
					day = day + 30;
				}
				if(month <= 0){
					year = year - 1;
					month = month + 12;
				}
			}
			Date result = Date.valueOf(year + "-" + month + "-" + day);
			return result;
		case 'm':
			int numm = Integer.parseInt(String.valueOf(number));
			month = month - numm;
			if(month <= 0){
				year = year - 1;
				month = month + 12;
			}
			Date resultm = Date.valueOf(year + "-" + month + "-" + day);
			return resultm;
		case 'y':
			int numy = Integer.parseInt(String.valueOf(number));
			year = year - numy;
			Date resulty = Date.valueOf(year + "-" + month + "-" + day);
			return resulty;
		}
		return null;
	}

	/**
	 * lay so trang va so ket qua o trang cuoi
	 */
	@Override
	public int getNumberOfPage(int size) {
		// TODO Auto-generated method stub
		ConfigDTO config = configDAO.getLastConfig();
		int numberResultOfPage = config.getNumPerPage();
		int numberOfPage = (size / numberResultOfPage) + 1;
		int resultOfLastPage = size % numberResultOfPage;
		if(resultOfLastPage == 0) numberOfPage = numberOfPage - 1;
		return numberOfPage;
	}
	
	/**
	 * lay history va tra ve ket qua theo dang list
	 */
	@Override
	public List<LogTransferDTO> getLogTransferByCardNoAndStartDate(
			String cardNo, String subDate) {
		// TODO Auto-generated method stub
		Date startDate = calculateStartDate(subDate);
		return logTransferDAO.getLogFromInputDateToNowByCardNo(cardNo, startDate);
	}
	
	/**
	 * lay log type boi logtypeid
	 */
	@Override
	public String getLogTypeByLogTypeID(int logTypeID) {
		// TODO Auto-generated method stub
		return logTypeDAO.getLogTypeByLogTypeID(logTypeID);
	}

	/**
	 * lay ten khach hang bang accountID
	 */
	@Override
	public String getCustomerNameByAccountID(int accountID) {
		// TODO Auto-generated method stub
		CustomerDTO customer = customerDAO.getCustomerByAccountID(accountID);
		return customer.getName();
	}
	
	/**
	 * lay dia chi atm
	 */
	@Override
	public String getATMAddressByATMID(int ATMID) {
		// TODO Auto-generated method stub
		ATMDTO atm = atmDAO.getATMByATMID(ATMID);
		return atm.getAddress();
	}
	
	/**
	 * lay log view show len man hinh
	 */
	@Override
	public List<LogViewDTO> getLogViewByATMIDAndCardNoAndTimeAgo(
			String cardNo, String timeAgo, int currentPage) {
		// TODO Auto-generated method stub
		List<LogTransferDTO> list = getLogTransferByCardNoAndStartDate(cardNo, timeAgo);
		List<LogViewDTO> viewList = new ArrayList<>();
		int maxPage = getNumberOfPage(list.size());
		int numberResultAPage = configDAO.getLastConfig().getNumPerPage();
		for(int i = ((currentPage - 1) * numberResultAPage), j = i + 5 ; i < j ; i++){
			if(i >= list.size()) break;
			LogTransferDTO logTransfer = list.get(i);
			double amount = logTransfer.getAmount();
			if(amount < 0) amount = 0;
			String newAmount = amount + "";
			String toAccount;
			int toAcc = logTransfer.getToAccountID();
			if(toAcc <= 0) toAccount = "";
			else{
				toAccount = getCustomerNameByAccountID(toAcc);
			}
			LogViewDTO logview = new LogViewDTO(getATMAddressByATMID(logTransfer.getAtmID()), getLogTypeByLogTypeID(logTransfer.getLogTypeID()), logTransfer.getLogDate().toString(), newAmount, toAccount);
			viewList.add(logview);
		}
		if(!viewList.isEmpty()) return viewList;
		return null;
	}
	
	/**
	 * lay so trang toi da
	 */
	@Override
	public int getMaxPage(String cardNo, String timeAgo) {
		// TODO Auto-generated method stub
		List<LogTransferDTO> list = getLogTransferByCardNoAndStartDate(cardNo, timeAgo);
		if(null == list || list.isEmpty()) return -1;
		return getNumberOfPage(list.size());
	}
	
}
